#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys
import shutil

execfile(os.path.join(sys.path[0], 'framework.py'))

#from oriolus.trotter.core import PacketDescriptor
from oriolus.pith.publicator.stower import EmptyStower
from oriolus.pith.publicator.storer import Storer, StorerError
from unittest import TestCase, TestLoader, TextTestRunner

class TestStorer(TestCase):
    wget_path = ';' + os.path.abspath("../../../etc/Windows")
    
    def setUp(self):
        self.__base_dir = os.path.abspath("testdata")
        self.__store_path = os.path.join(self.__base_dir, "storer")
        self.__packet_dir = os.path.join(self.__base_dir, "packet_dir")
        self.__base_url = "ftp://dev.shtrih-m.ru/oriolus"
        
        if not os.path.exists(self.__packet_dir):
            os.makedirs(self.__packet_dir)
        
        self.env = os.getenv('PATH')
        os.putenv('PATH', self.env + self.wget_path)
             
    def tearDown(self):
        if os.path.exists(self.__store_path):
            shutil.rmtree(self.__store_path)

        if os.path.exists(self.__packet_dir):
            shutil.rmtree(self.__packet_dir)
             
    def test_store_packet_1(self):
        self.assertFalse(os.path.exists(self.__store_path))
        storer = Storer(self.__store_path)
        
        self.assertTrue(os.path.exists(self.__store_path))

    def test_store_packet_2(self):
        packet_path = os.path.join(self.__base_dir, "test_packet-1.2.3.4.zip.ori")
        
        storer = Storer(self.__store_path)
        storer.add_packet(packet_path, self.__base_url)
        
        self.assertTrue(os.path.exists(os.path.join(self.__store_path, "packetlist.yaml")))
        
        store_packet_path = os.path.join(self.__store_path, "test_packet-1.2.3.4.zip.ori")
        self.assertTrue(os.path.exists(store_packet_path))
        self.assertTrue(storer.exists_packet(store_packet_path))

    def test_store_packet_3(self):
        packet_path_1 = os.path.join(self.__base_dir, "test_packet-1.2.3.4.zip.ori")
        packet_path_2 = os.path.join(self.__base_dir, "test_packet-2.3.4.5.zip.ori")
        packet_path_3 = os.path.join(self.__base_dir, "test_packet_2-1.2.3.4.zip.ori")
        
        storer = Storer(self.__store_path)
        storer.add_packet(packet_path_1, self.__base_url)
        storer.add_packet(packet_path_2, self.__base_url)
        storer.add_packet(packet_path_3, self.__base_url)
        storer.add_packet(packet_path_1, self.__base_url)
        
        self.__check_packets(storer)

    def test_store_packet_4(self):
        self.__make_packet_dir()
        
        storer = Storer(self.__store_path)
        storer.add_packet(self.__packet_dir, self.__base_url)
        
        self.__check_packets(storer)

    def test_store_packet_5(self):
        self.__make_packet_dir()
        
        packetlist_local = os.path.join(self.__base_dir, "packetlist.yaml")
        
        storer = Storer(self.__store_path, packetlist_local)
        storer.add_packet(self.__packet_dir, self.__base_url)
        
        self.__check_packets(storer)
        self.assertTrue(None != storer.get_packet_info("test_packet_3", "1.2.3.4"))

    def test_store_packet_6(self):
        self.__make_packet_dir()
        
        remote_packet_list_url = "ftp://dev.shtrih-m.ru/oriolus/test/"
        
        storer = Storer(self.__store_path, remote_packet_list_url = remote_packet_list_url)
        storer.add_packet(self.__packet_dir, self.__base_url)
        
        self.__check_packets(storer)
        self.assertTrue(None != storer.get_packet_info("shtrih_keys", "1.0.0.0"))

    def test_store_packet_7(self):
        self.__make_packet_dir()
        
        packetlist_local = os.path.join(self.__base_dir, "packetlist.yaml")
        remote_packet_list_url = "ftp://dev.shtrih-m.ru/oriolus/test/"
        
        storer = Storer(self.__store_path, packet_list_path = packetlist_local, remote_packet_list_url = remote_packet_list_url)
        storer.add_packet(self.__packet_dir, self.__base_url)
        
        self.__check_packets(storer)
        self.assertTrue(None != storer.get_packet_info("test_packet_3", "1.2.3.4"))
        self.assertTrue(None != storer.get_packet_info("shtrih_keys", "1.0.0.0"))

    def test_store_packet_missing_1(self):
        storer = Storer(self.__store_path)

        self.assertRaises(StorerError, Storer, self.__store_path, remote_packet_list_url = "ftp://invalid.url/repository")
        
    def test_store_packet_missing_2(self):
        packet_path = os.path.join(self.__base_dir, "test_packet_3-1.2.3.4.zip.ori")
        
        storer = Storer(self.__store_path)

        self.assertRaises(StorerError, storer.add_packet, packet_path, self.__base_url)
        
    def test_store_and_sign_packet_1(self):
        packet_path = os.path.join(self.__base_dir, "test_packet-1.2.3.4.zip.ori")
        
        storer = Storer(self.__store_path)
        storer.add_packet(packet_path, self.__base_url, sign = True, password = "Oriolus")
        
        self.assertTrue(os.path.exists(os.path.join(self.__store_path, "packetlist.yaml")))
        self.assertTrue(os.path.exists(os.path.join(self.__store_path, "packetlist.yaml.asc")))
        
        store_packet_path = os.path.join(self.__store_path, "test_packet-1.2.3.4.zip.ori")
        self.assertTrue(os.path.exists(store_packet_path))
        self.assertTrue(os.path.exists(store_packet_path + ".asc"))
        self.assertTrue(storer.exists_packet(store_packet_path))

    def test_store_and_sign_packet_2(self):
        packet_path_1 = os.path.join(self.__base_dir, "test_packet-1.2.3.4.zip.ori")
        packet_path_2 = os.path.join(self.__base_dir, "test_packet-2.3.4.5.zip.ori")
        packet_path_3 = os.path.join(self.__base_dir, "test_packet_2-1.2.3.4.zip.ori")
        shutil.copy(packet_path_1, self.__packet_dir)
        shutil.copy(packet_path_2, self.__packet_dir)
        shutil.copy(packet_path_3, self.__packet_dir)
        
        storer = Storer(self.__store_path)
        storer.add_packet(self.__packet_dir, self.__base_url, sign = True, password = "Oriolus")
        
        self.assertTrue(os.path.exists(os.path.join(self.__store_path, "packetlist.yaml")))
        self.assertTrue(os.path.exists(os.path.join(self.__store_path, "packetlist.yaml.asc")))
        
        store_packet_path_1 = os.path.join(self.__store_path, "test_packet-1.2.3.4.zip.ori")
        store_packet_path_2 = os.path.join(self.__store_path, "test_packet-2.3.4.5.zip.ori")
        store_packet_path_3 = os.path.join(self.__store_path, "test_packet_2-1.2.3.4.zip.ori")
        
        self.assertTrue(os.path.exists(store_packet_path_1))
        self.assertTrue(os.path.exists(store_packet_path_1 + ".asc"))
        self.assertTrue(storer.exists_packet(store_packet_path_1))
        self.assertTrue(os.path.exists(store_packet_path_2))
        self.assertTrue(os.path.exists(store_packet_path_2 + ".asc"))
        self.assertTrue(storer.exists_packet(store_packet_path_2))
        self.assertTrue(os.path.exists(store_packet_path_3))
        self.assertTrue(os.path.exists(store_packet_path_3 + ".asc"))
        self.assertTrue(storer.exists_packet(store_packet_path_3))
        
    def __make_packet_dir(self):
        packet_path_1 = os.path.join(self.__base_dir, "test_packet-1.2.3.4.zip.ori")
        packet_path_2 = os.path.join(self.__base_dir, "test_packet-2.3.4.5.zip.ori")
        packet_path_3 = os.path.join(self.__base_dir, "test_packet_2-1.2.3.4.zip.ori")
        shutil.copy(packet_path_1, self.__packet_dir)
        shutil.copy(packet_path_2, self.__packet_dir)
        shutil.copy(packet_path_3, self.__packet_dir)
        
    def __check_packets(self, storer):
        store_packet_path_1 = os.path.join(self.__store_path, "test_packet-1.2.3.4.zip.ori")
        store_packet_path_2 = os.path.join(self.__store_path, "test_packet-2.3.4.5.zip.ori")
        store_packet_path_3 = os.path.join(self.__store_path, "test_packet_2-1.2.3.4.zip.ori")
        
        self.assertTrue(os.path.exists(os.path.join(self.__store_path, "packetlist.yaml")))
        
        self.assertTrue(os.path.exists(store_packet_path_1))
        self.assertTrue(storer.exists_packet(store_packet_path_1))
        self.assertTrue(os.path.exists(store_packet_path_2))
        self.assertTrue(storer.exists_packet(store_packet_path_2))
        self.assertTrue(os.path.exists(store_packet_path_3))
        self.assertTrue(storer.exists_packet(store_packet_path_3))
        
if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestStorer)
    TextTestRunner(verbosity=2).run(suite)

